package com.huiyeji.dynamic.core.builder;

import com.huiyeji.dynamic.core.DynamicObjectInfo;
import com.huiyeji.dynamic.core.annotations.DynamicBean;
import com.huiyeji.dynamic.core.provider.DynamicAnnoMethodProvider;
import com.huiyeji.dynamic.core.provider.DynamicMethodProvider;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Huiyeji
 */
@DynamicBean("Static")
public class DynamicStaticObjectBuilder extends AbstractDynamicBuilder{
    @Override
    public DynamicObjectInfo initialize(Object object) {
        if (!(object instanceof String)){
            throw new RuntimeException("The parameters for Static Builder must be fully qualified class names");
        }

        try{
            Class<?> aClass = Class.forName((String) object);
            return new DynamicObjectInfo(aClass.getSimpleName(),aClass);
        }catch (Exception exception){
            throw new RuntimeException(exception);
        }
    }

    @Override
    public DynamicMethodProvider buildDynamicMethodProvider(DynamicObjectInfo info) {
        Class<?> aClass = (Class<?>) info.getObject();
        Method[] methods = aClass.getMethods();
        List<Method> provideMethods = new ArrayList<>();
        for (Method method : methods) {
            if (Modifier.isStatic(method.getModifiers())){
                provideMethods.add(method);
            }
        }
        info.setObject(null);
        return new DynamicAnnoMethodProvider(provideMethods);
    }
}
